package cn.jhc.sevlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.bean.Clazz;
import cn.jhc.dao.ClassDao;
import cn.jhc.dao.impl.ClassDaoImpl;

/**
 * Servlet implementation class AddClassServlet
 */
@WebServlet("/user/teacher/addclass.do")
public class AddClassServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddClassServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		
		boolean flag;
		try{
			String classname = request.getParameter("classname");
			String specialty = request.getParameter("specialty");
			String college = request.getParameter("college");
			ClassDao classDao = new ClassDaoImpl();
			Clazz clazz = new Clazz();
			clazz.setName(classname);
			clazz.setSpecialty(specialty);
			clazz.setCollege(college);
			classDao.save(clazz);
			flag = true;
		} catch(Exception e) {
			flag = false;
		}
		
		if(flag){
			response.sendRedirect("../index.jsp");
		} else{
			request.setAttribute("flag", false);
			request.getRequestDispatcher("addclass.jsp").forward(request,
					response);
		}
	}

}
